package com.jiinfo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.core.ActionKey;
import com.jfinal.plugin.activerecord.Record;
import com.jiinfo.base.BaseController;
import com.jiinfo.model.Organ;
import com.jiinfo.vo.BaseVo;

public class OrganController extends BaseController{
	
	private static final Logger logger = LoggerFactory.getLogger(OrganController.class);
	
	@ActionKey("/manage/organ/")
	public void manage_organ(){
		
		String childName="child";
		List<Record> organTree=Organ.dao.getTreeorgans(childName);
		List<Record> list=new ArrayList<>();
		for (Record record : organTree) {
			getListForTree(childName,record,list);
		}
		
		setAttr("organList", list);
		setAttr("pageTitle", getPageTitle("机构管理", "",
				new String[]{"<i class='icon-home'></i>首页","机构管理"},
				new String[]{"/manage","/manage/organ/"}));
		renderJsp("/manage/organ/organ-list.jsp");
		
	}
	
	
	@ActionKey("/manage/organ/add/")
	public void manage_add(){
		Record user=getCurrUser();
		
		Organ vo=getModel(Organ.class,"vo");
		Organ.dao.add(vo,user);
		
		redirect("/manage/organ/");
		
	}
	
	@ActionKey("/manage/organ/edit/")
	public void manage_edit(){
		Record user=getCurrUser();
		
		Organ vo=getModel(Organ.class,"vo");
		Organ.dao.edit(vo,user);
		
		redirect("/manage/organ/");
		
	}
	
	@ActionKey("/manage/organ/remove/")
	public void manage_remove(){
		
		String id=getPara("id");
		Organ.dao.remove(id);
		redirect("/manage/organ/");
		
	}
	
	@ActionKey("/manage/organ/viewJson/")
	public void manage_viewJson(){
		
		String id=getPara("id");
		
		Map<String,Object> map=new HashMap<>();
		Organ organ=Organ.dao.findById(id);
		Organ organParent=Organ.dao.findById(organ.getStr("parentId"));
		map.put("organ", organ);
		map.put("organParent", organParent);
		
		BaseVo baseVo=new BaseVo();
		baseVo.setFlag(true);
		baseVo.setData(map);
		
		renderJson(baseVo);
		
	}

}
